AOMedia AV1 Codec
aomdec
1/*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <stdarg.h>
15#include <string.h>
16#include <limits.h>
17
18#include "config/aom_config.h"
19
20#if CONFIG_OS_SUPPORT
21#if HAVE_UNISTD_H
22#include <unistd.h> // NOLINT
23#elif !defined(STDOUT_FILENO)
24#define STDOUT_FILENO 1
25#endif
26#endif
27
28#include "aom/aom_decoder.h"
29#include "aom/aomdx.h"
30#include "aom_ports/aom_timer.h"
31#include "aom_ports/mem_ops.h"
32#include "common/args.h"
33#include "common/ivfdec.h"
34#include "common/md5_utils.h"
35#include "common/obudec.h"
36#include "common/tools_common.h"
37
38#if CONFIG_WEBM_IO
39#include "common/webmdec.h"
40#endif
41
42#include "common/rawenc.h"
43#include "common/y4menc.h"
44
45#if CONFIG_LIBYUV
46#include "third_party/libyuv/include/libyuv/scale.h"
47#endif
48
49static const char *exec_name;
50
51struct AvxDecInputContext {
52 struct AvxInputContext *aom_input_ctx;
53 struct ObuDecInputContext *obu_ctx;
54 struct WebmInputContext *webm_ctx;
55};
56
57static const arg_def_t help =
58 ARG_DEF(NULL, "help", 0, "Show usage options and exit");
59static const arg_def_t looparg =
60 ARG_DEF(NULL, "loops", 1, "Number of times to decode the file");
61static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1, "Codec to use");
62static const arg_def_t use_yv12 =
63 ARG_DEF(NULL, "yv12", 0, "Output raw YV12 frames");
64static const arg_def_t use_i420 =
65 ARG_DEF(NULL, "i420", 0, "Output raw I420 frames");
66static const arg_def_t flipuvarg =
67 ARG_DEF(NULL, "flipuv", 0, "Flip the chroma planes in the output");
68static const arg_def_t rawvideo =
69 ARG_DEF(NULL, "rawvideo", 0, "Output raw YUV frames");
70static const arg_def_t noblitarg =
71 ARG_DEF(NULL, "noblit", 0, "Don't process the decoded frames");
72static const arg_def_t progressarg =
73 ARG_DEF(NULL, "progress", 0, "Show progress after each frame decodes");
74static const arg_def_t limitarg =
75 ARG_DEF(NULL, "limit", 1, "Stop decoding after n frames");
76static const arg_def_t skiparg =
77 ARG_DEF(NULL, "skip", 1, "Skip the first n input frames");
78static const arg_def_t summaryarg =
79 ARG_DEF(NULL, "summary", 0, "Show timing summary");
80static const arg_def_t outputfile =
81 ARG_DEF("o", "output", 1, "Output file name pattern (see below)");
82static const arg_def_t threadsarg =
83 ARG_DEF("t", "threads", 1, "Max threads to use");
84static const arg_def_t rowmtarg =
85 ARG_DEF(NULL, "row-mt", 1, "Enable row based multi-threading, default: 0");
86static const arg_def_t verbosearg =
87 ARG_DEF("v", "verbose", 0, "Show version string");
88static const arg_def_t scalearg =
89 ARG_DEF("S", "scale", 0, "Scale output frames uniformly");
90static const arg_def_t continuearg =
91 ARG_DEF("k", "keep-going", 0, "(debug) Continue decoding after error");
92static const arg_def_t fb_arg =
93 ARG_DEF(NULL, "frame-buffers", 1, "Number of frame buffers to use");
94static const arg_def_t md5arg =
95 ARG_DEF(NULL, "md5", 0, "Compute the MD5 sum of the decoded frame");
96static const arg_def_t framestatsarg =
97 ARG_DEF(NULL, "framestats", 1, "Output per-frame stats (.csv format)");
98static const arg_def_t outbitdeptharg =
99 ARG_DEF(NULL, "output-bit-depth", 1, "Output bit-depth for decoded frames");
100static const arg_def_t isannexb =
101 ARG_DEF(NULL, "annexb", 0, "Bitstream is in Annex-B format");
102static const arg_def_t oppointarg = ARG_DEF(
103 NULL, "oppoint", 1, "Select an operating point of a scalable bitstream");
104static const arg_def_t outallarg = ARG_DEF(
105 NULL, "all-layers", 0, "Output all decoded frames of a scalable bitstream");
106static const arg_def_t skipfilmgrain =
107 ARG_DEF(NULL, "skip-film-grain", 0, "Skip film grain application");
108
109static const arg_def_t *all_args[] = {
110 &help, &codecarg, &use_yv12, &use_i420,
111 &flipuvarg, &rawvideo, &noblitarg, &progressarg,
112 &limitarg, &skiparg, &summaryarg, &outputfile,
113 &threadsarg, &rowmtarg, &verbosearg, &scalearg,
114 &fb_arg, &md5arg, &framestatsarg, &continuearg,
115 &outbitdeptharg, &isannexb, &oppointarg, &outallarg,
116 &skipfilmgrain, NULL
117};
118
119#if CONFIG_LIBYUV
120// Returns 0 on success and returns -1 on failure.
121static inline int libyuv_scale(const aom_image_t *src, aom_image_t *dst,
122 FilterModeEnum mode) {
123 if (src->fmt != dst->fmt) {
124 fprintf(stderr,
125 "%s failed to scale output frame because format changed from %s to "
126 "%s\n",
127 exec_name, image_format_to_string(dst->fmt),
128 image_format_to_string(src->fmt));
129 return -1;
130 }
131 if (src->fmt == AOM_IMG_FMT_I42016) {
132 return I420Scale_16(
133 (uint16_t *)src->planes[AOM_PLANE_Y], src->stride[AOM_PLANE_Y] / 2,
134 (uint16_t *)src->planes[AOM_PLANE_U], src->stride[AOM_PLANE_U] / 2,
135 (uint16_t *)src->planes[AOM_PLANE_V], src->stride[AOM_PLANE_V] / 2,
136 src->d_w, src->d_h, (uint16_t *)dst->planes[AOM_PLANE_Y],
137 dst->stride[AOM_PLANE_Y] / 2, (uint16_t *)dst->planes[AOM_PLANE_U],
138 dst->stride[AOM_PLANE_U] / 2, (uint16_t *)dst->planes[AOM_PLANE_V],
139 dst->stride[AOM_PLANE_V] / 2, dst->d_w, dst->d_h, mode);
140 }
141 if (src->fmt == AOM_IMG_FMT_I420) {
142 return I420Scale(src->planes[AOM_PLANE_Y], src->stride[AOM_PLANE_Y],
145 src->d_w, src->d_h, dst->planes[AOM_PLANE_Y],
148 dst->stride[AOM_PLANE_V], dst->d_w, dst->d_h, mode);
149 }
150 fprintf(stderr, "%s cannot scale output frame of format %s\n", exec_name,
151 image_format_to_string(src->fmt));
152 return -1;
153}
154#endif
155
156static void show_help(FILE *fout, int shorthelp) {
157 fprintf(fout, "Usage: %s <options> filename\n\n", exec_name);
158
159 if (shorthelp) {
160 fprintf(fout, "Use --help to see the full list of options.\n");
161 return;
162 }
163
164 fprintf(fout, "Options:\n");
165 arg_show_usage(fout, all_args);
166 fprintf(fout,
167 "\nOutput File Patterns:\n\n"
168 " The -o argument specifies the name of the file(s) to "
169 "write to. If the\n argument does not include any escape "
170 "characters, the output will be\n written to a single file. "
171 "Otherwise, the filename will be calculated by\n expanding "
172 "the following escape characters:\n");
173 fprintf(fout,
174 "\n\t%%w - Frame width"
175 "\n\t%%h - Frame height"
176 "\n\t%%<n> - Frame number, zero padded to <n> places (1..9)"
177 "\n\n Pattern arguments are only supported in conjunction "
178 "with the --yv12 and\n --i420 options. If the -o option is "
179 "not specified, the output will be\n directed to stdout.\n");
180 fprintf(fout, "\nIncluded decoders:\n\n");
181
182 for (int i = 0; i < get_aom_decoder_count(); ++i) {
183 aom_codec_iface_t *decoder = get_aom_decoder_by_index(i);
184 fprintf(fout, " %-6s - %s\n", get_short_name_by_aom_decoder(decoder),
185 aom_codec_iface_name(decoder));
186 }
187}
188
189void usage_exit(void) {
190 show_help(stderr, 1);
191 exit(EXIT_FAILURE);
192}
193
194static int raw_read_frame(struct AvxInputContext *input_ctx, uint8_t **buffer,
195 size_t *bytes_read, size_t *buffer_size) {
196 unsigned char raw_hdr[RAW_FRAME_HDR_SZ];
197 size_t frame_size = 0;
198
199 if (read_from_input(input_ctx, RAW_FRAME_HDR_SZ, raw_hdr) !=
200 RAW_FRAME_HDR_SZ) {
201 if (!input_eof(input_ctx))
202 aom_tools_warn("Failed to read RAW frame size\n");
203 } else {
204 const size_t kCorruptFrameThreshold = 256 * 1024 * 1024;
205 const size_t kFrameTooSmallThreshold = 256 * 1024;
206 frame_size = mem_get_le32(raw_hdr);
207
208 if (frame_size > kCorruptFrameThreshold) {
209 aom_tools_warn("Read invalid frame size (%u)\n",
210 (unsigned int)frame_size);
211 frame_size = 0;
212 }
213
214 if (frame_size < kFrameTooSmallThreshold) {
215 aom_tools_warn(
216 "Warning: Read invalid frame size (%u) - not a raw file?\n",
217 (unsigned int)frame_size);
218 }
219
220 if (frame_size > *buffer_size) {
221 uint8_t *new_buf = realloc(*buffer, 2 * frame_size);
222 if (new_buf) {
223 *buffer = new_buf;
224 *buffer_size = 2 * frame_size;
225 } else {
226 aom_tools_warn("Failed to allocate compressed data buffer\n");
227 frame_size = 0;
228 }
229 }
230 }
231
232 if (!input_eof(input_ctx)) {
233 if (read_from_input(input_ctx, frame_size, *buffer) != frame_size) {
234 aom_tools_warn("Failed to read full frame\n");
235 return 1;
236 }
237 *bytes_read = frame_size;
238 return 0;
239 }
240
241 return 1;
242}
243
244static int read_frame(struct AvxDecInputContext *input, uint8_t **buf,
245 size_t *bytes_in_buffer, size_t *buffer_size) {
246 switch (input->aom_input_ctx->file_type) {
247#if CONFIG_WEBM_IO
248 case FILE_TYPE_WEBM:
249 return webm_read_frame(input->webm_ctx, buf, bytes_in_buffer,
250 buffer_size);
251#endif
252 case FILE_TYPE_RAW:
253 return raw_read_frame(input->aom_input_ctx, buf, bytes_in_buffer,
254 buffer_size);
255 case FILE_TYPE_IVF:
256 return ivf_read_frame(input->aom_input_ctx, buf, bytes_in_buffer,
257 buffer_size, NULL);
258 case FILE_TYPE_OBU:
259 return obudec_read_temporal_unit(input->obu_ctx, buf, bytes_in_buffer,
260 buffer_size);
261 default: return 1;
262 }
263}
264
265static int file_is_raw(struct AvxInputContext *input) {
266 uint8_t buf[32];
267 int is_raw = 0;
269 memset(&si, 0, sizeof(si));
270
271 if (buffer_input(input, 32, buf, /*buffered=*/true) == 32) {
272 int i;
273
274 if (mem_get_le32(buf) < 256 * 1024 * 1024) {
275 for (i = 0; i < get_aom_decoder_count(); ++i) {
276 aom_codec_iface_t *decoder = get_aom_decoder_by_index(i);
277 if (!aom_codec_peek_stream_info(decoder, buf + 4, 32 - 4, &si)) {
278 is_raw = 1;
279 input->fourcc = get_fourcc_by_aom_decoder(decoder);
280 input->width = si.w;
281 input->height = si.h;
282 input->framerate.numerator = 30;
283 input->framerate.denominator = 1;
284 break;
285 }
286 }
287 }
288 }
289
290 rewind_detect(input);
291 return is_raw;
292}
293
294static void show_progress(int frame_in, int frame_out, uint64_t dx_time) {
295 fprintf(stderr,
296 "%d decoded frames/%d showed frames in %" PRId64 " us (%.2f fps)\r",
297 frame_in, frame_out, dx_time,
298 (double)frame_out * 1000000.0 / (double)dx_time);
299}
300
301struct ExternalFrameBuffer {
302 uint8_t *data;
303 size_t size;
304 int in_use;
305};
306
307struct ExternalFrameBufferList {
308 int num_external_frame_buffers;
309 struct ExternalFrameBuffer *ext_fb;
310};
311
312// Callback used by libaom to request an external frame buffer. |cb_priv|
313// Application private data passed into the set function. |min_size| is the
314// minimum size in bytes needed to decode the next frame. |fb| pointer to the
315// frame buffer.
316static int get_av1_frame_buffer(void *cb_priv, size_t min_size,
318 int i;
319 struct ExternalFrameBufferList *const ext_fb_list =
320 (struct ExternalFrameBufferList *)cb_priv;
321 if (ext_fb_list == NULL) return -1;
322
323 // Find a free frame buffer.
324 for (i = 0; i < ext_fb_list->num_external_frame_buffers; ++i) {
325 if (!ext_fb_list->ext_fb[i].in_use) break;
326 }
327
328 if (i == ext_fb_list->num_external_frame_buffers) return -1;
329
330 if (ext_fb_list->ext_fb[i].size < min_size) {
331 free(ext_fb_list->ext_fb[i].data);
332 ext_fb_list->ext_fb[i].data = (uint8_t *)calloc(min_size, sizeof(uint8_t));
333 if (!ext_fb_list->ext_fb[i].data) return -1;
334
335 ext_fb_list->ext_fb[i].size = min_size;
336 }
337
338 fb->data = ext_fb_list->ext_fb[i].data;
339 fb->size = ext_fb_list->ext_fb[i].size;
340 ext_fb_list->ext_fb[i].in_use = 1;
341
342 // Set the frame buffer's private data to point at the external frame buffer.
343 fb->priv = &ext_fb_list->ext_fb[i];
344 return 0;
345}
346
347// Callback used by libaom when there are no references to the frame buffer.
348// |cb_priv| user private data passed into the set function. |fb| pointer
349// to the frame buffer.
350static int release_av1_frame_buffer(void *cb_priv,
352 struct ExternalFrameBuffer *const ext_fb =
353 (struct ExternalFrameBuffer *)fb->priv;
354 (void)cb_priv;
355 ext_fb->in_use = 0;
356 return 0;
357}
358
359static void generate_filename(const char *pattern, char *out, size_t q_len,
360 unsigned int d_w, unsigned int d_h,
361 unsigned int frame_in) {
362 const char *p = pattern;
363 char *q = out;
364
365 do {
366 char *next_pat = strchr(p, '%');
367
368 if (p == next_pat) {
369 size_t pat_len;
370
371 /* parse the pattern */
372 q[q_len - 1] = '\0';
373 switch (p[1]) {
374 case 'w': snprintf(q, q_len - 1, "%d", d_w); break;
375 case 'h': snprintf(q, q_len - 1, "%d", d_h); break;
376 case '1': snprintf(q, q_len - 1, "%d", frame_in); break;
377 case '2': snprintf(q, q_len - 1, "%02d", frame_in); break;
378 case '3': snprintf(q, q_len - 1, "%03d", frame_in); break;
379 case '4': snprintf(q, q_len - 1, "%04d", frame_in); break;
380 case '5': snprintf(q, q_len - 1, "%05d", frame_in); break;
381 case '6': snprintf(q, q_len - 1, "%06d", frame_in); break;
382 case '7': snprintf(q, q_len - 1, "%07d", frame_in); break;
383 case '8': snprintf(q, q_len - 1, "%08d", frame_in); break;
384 case '9': snprintf(q, q_len - 1, "%09d", frame_in); break;
385 default: die("Unrecognized pattern %%%c\n", p[1]);
386 }
387
388 pat_len = strlen(q);
389 if (pat_len >= q_len - 1) die("Output filename too long.\n");
390 q += pat_len;
391 p += 2;
392 q_len -= pat_len;
393 } else {
394 size_t copy_len;
395
396 /* copy the next segment */
397 if (!next_pat)
398 copy_len = strlen(p);
399 else
400 copy_len = next_pat - p;
401
402 if (copy_len >= q_len - 1) die("Output filename too long.\n");
403
404 memcpy(q, p, copy_len);
405 q[copy_len] = '\0';
406 q += copy_len;
407 p += copy_len;
408 q_len -= copy_len;
409 }
410 } while (*p);
411}
412
413static int is_single_file(const char *outfile_pattern) {
414 const char *p = outfile_pattern;
415
416 do {
417 p = strchr(p, '%');
418 if (p && p[1] >= '1' && p[1] <= '9')
419 return 0; // pattern contains sequence number, so it's not unique
420 if (p) p++;
421 } while (p);
422
423 return 1;
424}
425
426static void print_md5(unsigned char digest[16], const char *filename) {
427 int i;
428
429 for (i = 0; i < 16; ++i) printf("%02x", digest[i]);
430 printf(" %s\n", filename);
431}
432
433static FILE *open_outfile(const char *name) {
434 if (strcmp("-", name) == 0) {
435 set_binary_mode(stdout);
436 return stdout;
437 } else {
438 FILE *file = fopen(name, "wb");
439 if (!file) fatal("Failed to open output file '%s'", name);
440 return file;
441 }
442}
443
444static int main_loop(int argc, const char **argv_) {
445 aom_codec_ctx_t decoder;
446 char *fn = NULL;
447 int i;
448 int ret = EXIT_FAILURE;
449 uint8_t *buf = NULL;
450 size_t bytes_in_buffer = 0, buffer_size = 0;
451 FILE *infile;
452 int frame_in = 0, frame_out = 0, flipuv = 0, noblit = 0;
453 int do_md5 = 0, progress = 0;
454 int stop_after = 0, summary = 0, quiet = 1;
455 int arg_skip = 0;
456 int keep_going = 0;
457 uint64_t dx_time = 0;
458 struct arg arg;
459 char **argv, **argi, **argj;
460
461 int single_file;
462 int use_y4m = 1;
463 int opt_yv12 = 0;
464 int opt_i420 = 0;
465 int opt_raw = 0;
466 aom_codec_dec_cfg_t cfg = { 0, 0, 0, !FORCE_HIGHBITDEPTH_DECODING };
467 unsigned int fixed_output_bit_depth = 0;
468 unsigned int is_annexb = 0;
469 int frames_corrupted = 0;
470 int dec_flags = 0;
471 int do_scale = 0;
472 int operating_point = 0;
473 int output_all_layers = 0;
474 int skip_film_grain = 0;
475 int enable_row_mt = 0;
476 aom_image_t *scaled_img = NULL;
477 aom_image_t *img_shifted = NULL;
478 int frame_avail, got_data, flush_decoder = 0;
479 int num_external_frame_buffers = 0;
480 struct ExternalFrameBufferList ext_fb_list = { 0, NULL };
481
482 const char *outfile_pattern = NULL;
483 char outfile_name[PATH_MAX] = { 0 };
484 FILE *outfile = NULL;
485
486 FILE *framestats_file = NULL;
487
488 MD5Context md5_ctx;
489 unsigned char md5_digest[16];
490
491 struct AvxDecInputContext input = { NULL, NULL, NULL };
492 struct AvxInputContext aom_input_ctx;
493 memset(&aom_input_ctx, 0, sizeof(aom_input_ctx));
494#if CONFIG_WEBM_IO
495 struct WebmInputContext webm_ctx;
496 memset(&webm_ctx, 0, sizeof(webm_ctx));
497 input.webm_ctx = &webm_ctx;
498#endif
499 struct ObuDecInputContext obu_ctx = { NULL, NULL, 0, 0, 0 };
500 int is_ivf = 0;
501
502 obu_ctx.avx_ctx = &aom_input_ctx;
503 input.obu_ctx = &obu_ctx;
504 input.aom_input_ctx = &aom_input_ctx;
505
506 /* Parse command line */
507 exec_name = argv_[0];
508 argv = argv_dup(argc - 1, argv_ + 1);
509 if (!argv) {
510 fprintf(stderr, "Error allocating argument list\n");
511 return EXIT_FAILURE;
512 }
513
514 aom_codec_iface_t *interface = NULL;
515 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
516 memset(&arg, 0, sizeof(arg));
517 arg.argv_step = 1;
518
519 if (arg_match(&arg, &help, argi)) {
520 show_help(stdout, 0);
521 exit(EXIT_SUCCESS);
522 } else if (arg_match(&arg, &codecarg, argi)) {
523 interface = get_aom_decoder_by_short_name(arg.val);
524 if (!interface)
525 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
526 } else if (arg_match(&arg, &looparg, argi)) {
527 // no-op
528 } else if (arg_match(&arg, &outputfile, argi)) {
529 outfile_pattern = arg.val;
530 } else if (arg_match(&arg, &use_yv12, argi)) {
531 use_y4m = 0;
532 flipuv = 1;
533 opt_yv12 = 1;
534 opt_i420 = 0;
535 opt_raw = 0;
536 } else if (arg_match(&arg, &use_i420, argi)) {
537 use_y4m = 0;
538 flipuv = 0;
539 opt_yv12 = 0;
540 opt_i420 = 1;
541 opt_raw = 0;
542 } else if (arg_match(&arg, &rawvideo, argi)) {
543 use_y4m = 0;
544 opt_yv12 = 0;
545 opt_i420 = 0;
546 opt_raw = 1;
547 } else if (arg_match(&arg, &flipuvarg, argi)) {
548 flipuv = 1;
549 } else if (arg_match(&arg, &noblitarg, argi)) {
550 noblit = 1;
551 } else if (arg_match(&arg, &progressarg, argi)) {
552 progress = 1;
553 } else if (arg_match(&arg, &limitarg, argi)) {
554 stop_after = arg_parse_uint(&arg);
555 } else if (arg_match(&arg, &skiparg, argi)) {
556 arg_skip = arg_parse_uint(&arg);
557 } else if (arg_match(&arg, &md5arg, argi)) {
558 do_md5 = 1;
559 } else if (arg_match(&arg, &framestatsarg, argi)) {
560 framestats_file = fopen(arg.val, "w");
561 if (!framestats_file) {
562 die("Error: Could not open --framestats file (%s) for writing.\n",
563 arg.val);
564 }
565 } else if (arg_match(&arg, &summaryarg, argi)) {
566 summary = 1;
567 } else if (arg_match(&arg, &threadsarg, argi)) {
568 cfg.threads = arg_parse_uint(&arg);
569#if !CONFIG_MULTITHREAD
570 if (cfg.threads > 1) {
571 die("Error: --threads=%d is not supported when CONFIG_MULTITHREAD = "
572 "0.\n",
573 cfg.threads);
574 }
575#endif
576 } else if (arg_match(&arg, &rowmtarg, argi)) {
577 enable_row_mt = arg_parse_uint(&arg);
578 } else if (arg_match(&arg, &verbosearg, argi)) {
579 quiet = 0;
580 } else if (arg_match(&arg, &scalearg, argi)) {
581 do_scale = 1;
582 } else if (arg_match(&arg, &fb_arg, argi)) {
583 num_external_frame_buffers = arg_parse_uint(&arg);
584 } else if (arg_match(&arg, &continuearg, argi)) {
585 keep_going = 1;
586 } else if (arg_match(&arg, &outbitdeptharg, argi)) {
587 fixed_output_bit_depth = arg_parse_uint(&arg);
588 } else if (arg_match(&arg, &isannexb, argi)) {
589 is_annexb = 1;
590 input.obu_ctx->is_annexb = 1;
591 } else if (arg_match(&arg, &oppointarg, argi)) {
592 operating_point = arg_parse_int(&arg);
593 } else if (arg_match(&arg, &outallarg, argi)) {
594 output_all_layers = 1;
595 } else if (arg_match(&arg, &skipfilmgrain, argi)) {
596 skip_film_grain = 1;
597 } else {
598 argj++;
599 }
600 }
601
602 /* Check for unrecognized options */
603 for (argi = argv; *argi; argi++)
604 if (argi[0][0] == '-' && strlen(argi[0]) > 1)
605 die("Error: Unrecognized option %s\n", *argi);
606
607 /* Handle non-option arguments */
608 fn = argv[0];
609
610 if (!fn) {
611 free(argv);
612 fprintf(stderr, "No input file specified!\n");
613 usage_exit();
614 }
615
616 const bool using_file = strcmp(fn, "-") != 0;
617 /* Open file */
618 infile = using_file ? fopen(fn, "rb") : set_binary_mode(stdin);
619
620 if (!infile) {
621 fatal("Failed to open input file '%s'", using_file ? fn : "stdin");
622 }
623#if CONFIG_OS_SUPPORT
624 /* Make sure we don't dump to the terminal, unless forced to with -o - */
625 if (!outfile_pattern && isatty(STDOUT_FILENO) && !do_md5 && !noblit) {
626 fprintf(stderr,
627 "Not dumping raw video to your terminal. Use '-o -' to "
628 "override.\n");
629 free(argv);
630 return EXIT_FAILURE;
631 }
632#endif
633 input.aom_input_ctx->filename = fn;
634 input.aom_input_ctx->file = infile;
635
636 // TODO(https://crbug.com/aomedia/1706): webm type does not support reading
637 // from stdin yet, and file_is_webm is not using the detect buffer when
638 // determining the type. Therefore it should only be checked when using a file
639 // and needs to be checked prior to other types.
640 if (false) {
641#if CONFIG_WEBM_IO
642 } else if (using_file && file_is_webm(input.webm_ctx, input.aom_input_ctx)) {
643 input.aom_input_ctx->file_type = FILE_TYPE_WEBM;
644#endif
645 } else if (file_is_ivf(input.aom_input_ctx)) {
646 input.aom_input_ctx->file_type = FILE_TYPE_IVF;
647 is_ivf = 1;
648 } else if (file_is_obu(&obu_ctx)) {
649 input.aom_input_ctx->file_type = FILE_TYPE_OBU;
650 } else if (file_is_raw(input.aom_input_ctx)) {
651 input.aom_input_ctx->file_type = FILE_TYPE_RAW;
652 } else {
653 fprintf(stderr, "Unrecognized input file type.\n");
654#if CONFIG_WEBM_IO
655 if (!using_file) {
656 fprintf(stderr, "aomdec does not support piped WebM input.\n");
657 }
658#else
659 fprintf(stderr, "aomdec was built without WebM container support.\n");
660#endif
661 free(argv);
662 return EXIT_FAILURE;
663 }
664
665 outfile_pattern = outfile_pattern ? outfile_pattern : "-";
666 single_file = is_single_file(outfile_pattern);
667
668 if (!noblit && single_file) {
669 generate_filename(outfile_pattern, outfile_name, PATH_MAX,
670 aom_input_ctx.width, aom_input_ctx.height, 0);
671 if (do_md5)
672 MD5Init(&md5_ctx);
673 else
674 outfile = open_outfile(outfile_name);
675 }
676
677 if (use_y4m && !noblit) {
678 if (!single_file) {
679 fprintf(stderr,
680 "YUV4MPEG2 not supported with output patterns,"
681 " try --i420 or --yv12 or --rawvideo.\n");
682 return EXIT_FAILURE;
683 }
684
685#if CONFIG_WEBM_IO
686 if (aom_input_ctx.file_type == FILE_TYPE_WEBM) {
687 if (webm_guess_framerate(input.webm_ctx, input.aom_input_ctx)) {
688 fprintf(stderr,
689 "Failed to guess framerate -- error parsing "
690 "webm file?\n");
691 return EXIT_FAILURE;
692 }
693 }
694#endif
695 }
696
697 aom_codec_iface_t *fourcc_interface =
698 get_aom_decoder_by_fourcc(aom_input_ctx.fourcc);
699
700 if (is_ivf && !fourcc_interface)
701 fatal("Unsupported fourcc: %x\n", aom_input_ctx.fourcc);
702
703 if (interface && fourcc_interface && interface != fourcc_interface)
704 aom_tools_warn("Header indicates codec: %s\n",
705 aom_codec_iface_name(fourcc_interface));
706 else
707 interface = fourcc_interface;
708
709 if (!interface) interface = get_aom_decoder_by_index(0);
710
711 dec_flags = 0;
712 if (aom_codec_dec_init(&decoder, interface, &cfg, dec_flags)) {
713 fprintf(stderr, "Failed to initialize decoder: %s\n",
714 aom_codec_error(&decoder));
715 goto fail2;
716 }
717
718 if (!quiet) fprintf(stderr, "%s\n", decoder.name);
719
720 if (AOM_CODEC_CONTROL_TYPECHECKED(&decoder, AV1D_SET_IS_ANNEXB, is_annexb)) {
721 fprintf(stderr, "Failed to set is_annexb: %s\n", aom_codec_error(&decoder));
722 goto fail;
723 }
724
726 operating_point)) {
727 fprintf(stderr, "Failed to set operating_point: %s\n",
728 aom_codec_error(&decoder));
729 goto fail;
730 }
731
733 output_all_layers)) {
734 fprintf(stderr, "Failed to set output_all_layers: %s\n",
735 aom_codec_error(&decoder));
736 goto fail;
737 }
738
740 skip_film_grain)) {
741 fprintf(stderr, "Failed to set skip_film_grain: %s\n",
742 aom_codec_error(&decoder));
743 goto fail;
744 }
745
746 if (AOM_CODEC_CONTROL_TYPECHECKED(&decoder, AV1D_SET_ROW_MT, enable_row_mt)) {
747 fprintf(stderr, "Failed to set row multithreading mode: %s\n",
748 aom_codec_error(&decoder));
749 goto fail;
750 }
751
752 if (arg_skip) fprintf(stderr, "Skipping first %d frames.\n", arg_skip);
753 while (arg_skip) {
754 if (read_frame(&input, &buf, &bytes_in_buffer, &buffer_size)) break;
755 arg_skip--;
756 }
757
758 if (num_external_frame_buffers > 0) {
759 ext_fb_list.num_external_frame_buffers = num_external_frame_buffers;
760 ext_fb_list.ext_fb = (struct ExternalFrameBuffer *)calloc(
761 num_external_frame_buffers, sizeof(*ext_fb_list.ext_fb));
762 if (!ext_fb_list.ext_fb) {
763 fprintf(stderr, "Failed to allocate ExternalFrameBuffer\n");
764 goto fail;
765 }
766 if (aom_codec_set_frame_buffer_functions(&decoder, get_av1_frame_buffer,
767 release_av1_frame_buffer,
768 &ext_fb_list)) {
769 fprintf(stderr, "Failed to configure external frame buffers: %s\n",
770 aom_codec_error(&decoder));
771 goto fail;
772 }
773 }
774
775 frame_avail = 1;
776 got_data = 0;
777
778 if (framestats_file) fprintf(framestats_file, "bytes,qp\r\n");
779
780 /* Decode file */
781 while (frame_avail || got_data) {
782 aom_codec_iter_t iter = NULL;
783 aom_image_t *img;
784 struct aom_usec_timer timer;
785 int corrupted = 0;
786
787 frame_avail = 0;
788 if (!stop_after || frame_in < stop_after) {
789 if (!read_frame(&input, &buf, &bytes_in_buffer, &buffer_size)) {
790 frame_avail = 1;
791 frame_in++;
792
793 aom_usec_timer_start(&timer);
794
795 if (aom_codec_decode(&decoder, buf, bytes_in_buffer, NULL)) {
796 const char *detail = aom_codec_error_detail(&decoder);
797 aom_tools_warn("Failed to decode frame %d: %s", frame_in,
798 aom_codec_error(&decoder));
799
800 if (detail) aom_tools_warn("Additional information: %s", detail);
801 if (!keep_going) goto fail;
802 }
803
804 if (framestats_file) {
805 int qp;
807 &qp)) {
808 aom_tools_warn("Failed AOMD_GET_LAST_QUANTIZER: %s",
809 aom_codec_error(&decoder));
810 if (!keep_going) goto fail;
811 }
812 fprintf(framestats_file, "%d,%d\r\n", (int)bytes_in_buffer, qp);
813 }
814
815 aom_usec_timer_mark(&timer);
816 dx_time += aom_usec_timer_elapsed(&timer);
817 } else {
818 flush_decoder = 1;
819 }
820 } else {
821 flush_decoder = 1;
822 }
823
824 aom_usec_timer_start(&timer);
825
826 if (flush_decoder) {
827 // Flush the decoder.
828 if (aom_codec_decode(&decoder, NULL, 0, NULL)) {
829 aom_tools_warn("Failed to flush decoder: %s",
830 aom_codec_error(&decoder));
831 }
832 }
833
834 aom_usec_timer_mark(&timer);
835 dx_time += aom_usec_timer_elapsed(&timer);
836
837 got_data = 0;
838 // TODO(aomedia:3519): Change the prototype of aom_codec_get_frame_fn_t to
839 // facilitate error handling.
840 while ((img = aom_codec_get_frame(&decoder, &iter))) {
841 ++frame_out;
842 got_data = 1;
843
845 &corrupted)) {
846 aom_tools_warn("Failed AOM_GET_FRAME_CORRUPTED: %s",
847 aom_codec_error(&decoder));
848 if (!keep_going) goto fail;
849 }
850 frames_corrupted += corrupted;
851
852 if (progress) show_progress(frame_in, frame_out, dx_time);
853
854 if (!noblit) {
855 const int PLANES_YUV[] = { AOM_PLANE_Y, AOM_PLANE_U, AOM_PLANE_V };
856 const int PLANES_YVU[] = { AOM_PLANE_Y, AOM_PLANE_V, AOM_PLANE_U };
857 const int *planes = flipuv ? PLANES_YVU : PLANES_YUV;
858
859 if (do_scale) {
860 if (frame_out == 1) {
861 // If the output frames are to be scaled to a fixed display size
862 // then use the width and height specified in the container. If
863 // either of these is set to 0, use the display size set in the
864 // first frame header. If that is unavailable, use the raw decoded
865 // size of the first decoded frame.
866 int render_width = aom_input_ctx.width;
867 int render_height = aom_input_ctx.height;
868 if (!render_width || !render_height) {
869 int render_size[2];
871 render_size)) {
872 // As last resort use size of first frame as display size.
873 render_width = img->d_w;
874 render_height = img->d_h;
875 } else {
876 render_width = render_size[0];
877 render_height = render_size[1];
878 }
879 }
880 scaled_img =
881 aom_img_alloc(NULL, img->fmt, render_width, render_height, 16);
882 if (!scaled_img) {
883 fprintf(stderr, "Failed to allocate scaled image (%d x %d)\n",
884 render_width, render_height);
885 goto fail;
886 }
887 scaled_img->bit_depth = img->bit_depth;
888 scaled_img->monochrome = img->monochrome;
889 scaled_img->csp = img->csp;
890 }
891
892 if (img->d_w != scaled_img->d_w || img->d_h != scaled_img->d_h) {
893#if CONFIG_LIBYUV
894 if (libyuv_scale(img, scaled_img, kFilterBox) != 0) goto fail;
895 img = scaled_img;
896#else
897 fprintf(
898 stderr,
899 "Failed to scale output frame: %s.\n"
900 "libyuv is required for scaling but is currently disabled.\n"
901 "Be sure to specify -DCONFIG_LIBYUV=1 when running cmake.\n",
902 aom_codec_error(&decoder));
903 goto fail;
904#endif
905 }
906 }
907 // Default to codec bit depth if output bit depth not set
908 unsigned int output_bit_depth;
909 if (!fixed_output_bit_depth && single_file) {
910 output_bit_depth = img->bit_depth;
911 } else {
912 output_bit_depth = fixed_output_bit_depth;
913 }
914 // Shift up or down if necessary
915 if (output_bit_depth != 0) {
916 if (!aom_shift_img(output_bit_depth, &img, &img_shifted)) {
917 fprintf(stderr, "Error allocating image\n");
918 goto fail;
919 }
920 }
921
922 aom_input_ctx.width = img->d_w;
923 aom_input_ctx.height = img->d_h;
924
925 int num_planes = (opt_raw && img->monochrome) ? 1 : 3;
926 if (single_file) {
927 if (use_y4m) {
928 char y4m_buf[Y4M_BUFFER_SIZE] = { 0 };
929 size_t len = 0;
930 if (frame_out == 1) {
931 // Y4M file header
932 len = y4m_write_file_header(
933 y4m_buf, sizeof(y4m_buf), aom_input_ctx.width,
934 aom_input_ctx.height, &aom_input_ctx.framerate,
935 img->monochrome, img->csp, img->fmt, img->bit_depth,
936 img->range);
937 if (img->csp == AOM_CSP_COLOCATED) {
938 fprintf(stderr,
939 "Warning: Y4M lacks a colorspace for colocated "
940 "chroma. Using a placeholder.\n");
941 }
942 if (do_md5) {
943 MD5Update(&md5_ctx, (md5byte *)y4m_buf, (unsigned int)len);
944 } else {
945 fputs(y4m_buf, outfile);
946 }
947 }
948
949 // Y4M frame header
950 len = y4m_write_frame_header(y4m_buf, sizeof(y4m_buf));
951 if (do_md5) {
952 MD5Update(&md5_ctx, (md5byte *)y4m_buf, (unsigned int)len);
953 y4m_update_image_md5(img, planes, &md5_ctx);
954 } else {
955 fputs(y4m_buf, outfile);
956 y4m_write_image_file(img, planes, outfile);
957 }
958 } else {
959 if (frame_out == 1) {
960 // Check if --yv12 or --i420 options are consistent with the
961 // bit-stream decoded
962 if (opt_i420) {
963 if (img->fmt != AOM_IMG_FMT_I420 &&
964 img->fmt != AOM_IMG_FMT_I42016) {
965 fprintf(stderr,
966 "Cannot produce i420 output for bit-stream.\n");
967 goto fail;
968 }
969 }
970 if (opt_yv12) {
971 if ((img->fmt != AOM_IMG_FMT_I420 &&
972 img->fmt != AOM_IMG_FMT_YV12) ||
973 img->bit_depth != 8) {
974 fprintf(stderr,
975 "Cannot produce yv12 output for bit-stream.\n");
976 goto fail;
977 }
978 }
979 }
980 if (do_md5) {
981 raw_update_image_md5(img, planes, num_planes, &md5_ctx);
982 } else {
983 raw_write_image_file(img, planes, num_planes, outfile);
984 }
985 }
986 } else {
987 generate_filename(outfile_pattern, outfile_name, PATH_MAX, img->d_w,
988 img->d_h, frame_in);
989 if (do_md5) {
990 MD5Init(&md5_ctx);
991 if (use_y4m) {
992 y4m_update_image_md5(img, planes, &md5_ctx);
993 } else {
994 raw_update_image_md5(img, planes, num_planes, &md5_ctx);
995 }
996 MD5Final(md5_digest, &md5_ctx);
997 print_md5(md5_digest, outfile_name);
998 } else {
999 outfile = open_outfile(outfile_name);
1000 if (use_y4m) {
1001 y4m_write_image_file(img, planes, outfile);
1002 } else {
1003 raw_write_image_file(img, planes, num_planes, outfile);
1004 }
1005 fclose(outfile);
1006 }
1007 }
1008 }
1009 }
1010 }
1011
1012 if (summary || progress) {
1013 show_progress(frame_in, frame_out, dx_time);
1014 fprintf(stderr, "\n");
1015 }
1016
1017 if (frames_corrupted) {
1018 fprintf(stderr, "WARNING: %d frames corrupted.\n", frames_corrupted);
1019 } else {
1020 ret = EXIT_SUCCESS;
1021 }
1022
1023fail:
1024
1025 if (aom_codec_destroy(&decoder)) {
1026 fprintf(stderr, "Failed to destroy decoder: %s\n",
1027 aom_codec_error(&decoder));
1028 }
1029
1030fail2:
1031
1032 if (!noblit && single_file) {
1033 if (do_md5) {
1034 MD5Final(md5_digest, &md5_ctx);
1035 print_md5(md5_digest, outfile_name);
1036 } else {
1037 fclose(outfile);
1038 }
1039 }
1040
1041#if CONFIG_WEBM_IO
1042 if (input.aom_input_ctx->file_type == FILE_TYPE_WEBM)
1043 webm_free(input.webm_ctx);
1044#endif
1045 if (input.aom_input_ctx->file_type == FILE_TYPE_OBU)
1046 obudec_free(input.obu_ctx);
1047
1048 if (input.aom_input_ctx->file_type != FILE_TYPE_WEBM) free(buf);
1049
1050 if (scaled_img) aom_img_free(scaled_img);
1051 if (img_shifted) aom_img_free(img_shifted);
1052
1053 for (i = 0; i < ext_fb_list.num_external_frame_buffers; ++i) {
1054 free(ext_fb_list.ext_fb[i].data);
1055 }
1056 free(ext_fb_list.ext_fb);
1057
1058 fclose(infile);
1059 if (framestats_file) fclose(framestats_file);
1060
1061 free(argv);
1062
1063 return ret;
1064}
1065
1066int main(int argc, const char **argv_) {
1067 unsigned int loops = 1, i;
1068 char **argv, **argi, **argj;
1069 struct arg arg;
1070 int error = 0;
1071
1072 argv = argv_dup(argc - 1, argv_ + 1);
1073 if (!argv) {
1074 fprintf(stderr, "Error allocating argument list\n");
1075 return EXIT_FAILURE;
1076 }
1077 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
1078 memset(&arg, 0, sizeof(arg));
1079 arg.argv_step = 1;
1080
1081 if (arg_match(&arg, &looparg, argi)) {
1082 loops = arg_parse_uint(&arg);
1083 break;
1084 }
1085 }
1086 free(argv);
1087 for (i = 0; !error && i < loops; i++) error = main_loop(argc, argv_);
1088 return error;
1089}
Describes the decoder algorithm interface to applications.
struct aom_codec_frame_buffer aom_codec_frame_buffer_t
External frame buffer.
#define AOM_PLANE_U
Definition aom_image.h:227
@ AOM_CSP_COLOCATED
Definition aom_image.h:146
#define AOM_PLANE_Y
Definition aom_image.h:226
#define AOM_PLANE_V
Definition aom_image.h:228
aom_image_t * aom_img_alloc(aom_image_t *img, aom_img_fmt_t fmt, unsigned int d_w, unsigned int d_h, unsigned int align)
Open a descriptor, allocating storage for the underlying image.
@ AOM_IMG_FMT_I42016
Definition aom_image.h:56
@ AOM_IMG_FMT_I420
Definition aom_image.h:45
@ AOM_IMG_FMT_YV12
Definition aom_image.h:43
struct aom_image aom_image_t
Image Descriptor.
void aom_img_free(aom_image_t *img)
Close an image descriptor.
Provides definitions for using AOM or AV1 within the aom Decoder interface.
@ AOMD_GET_FRAME_CORRUPTED
Codec control function to check if the indicated frame is corrupted, int* parameter.
Definition aomdx.h:204
@ AV1D_SET_SKIP_FILM_GRAIN
Codec control function to set the skip film grain flag, int parameter.
Definition aomdx.h:390
@ AV1D_SET_IS_ANNEXB
Codec control function to indicate whether bitstream is in Annex-B format, unsigned int parameter.
Definition aomdx.h:352
@ AV1D_SET_ROW_MT
Codec control function to enable the row based multi-threading of decoding, unsigned int parameter.
Definition aomdx.h:347
@ AV1D_GET_DISPLAY_SIZE
Codec control function to get the current frame's intended display dimensions (as specified in the wr...
Definition aomdx.h:225
@ AV1D_SET_OUTPUT_ALL_LAYERS
Codec control function to indicate whether to output one frame per temporal unit (the default),...
Definition aomdx.h:374
@ AV1D_SET_OPERATING_POINT
Codec control function to indicate which operating point to use, int parameter.
Definition aomdx.h:362
@ AOMD_GET_LAST_QUANTIZER
Codec control function to get last decoded frame quantizer, int* parameter.
Definition aomdx.h:297
aom_codec_err_t aom_codec_set_frame_buffer_functions(aom_codec_ctx_t *ctx, aom_get_frame_buffer_cb_fn_t cb_get, aom_release_frame_buffer_cb_fn_t cb_release, void *cb_priv)
Pass in external frame buffers for the decoder to use.
const char * aom_codec_iface_name(aom_codec_iface_t *iface)
Return the name for a given interface.
struct aom_codec_ctx aom_codec_ctx_t
Codec context structure.
const struct aom_codec_iface aom_codec_iface_t
Codec interface structure.
Definition aom_codec.h:271
const char * aom_codec_error(const aom_codec_ctx_t *ctx)
Retrieve error synopsis for codec context.
aom_codec_err_t aom_codec_destroy(aom_codec_ctx_t *ctx)
Destroy a codec instance.
#define AOM_CODEC_CONTROL_TYPECHECKED(ctx, id, data)
aom_codec_control wrapper macro (adds type-checking, less flexible)
Definition aom_codec.h:542
const char * aom_codec_error_detail(const aom_codec_ctx_t *ctx)
Retrieve detailed error information for codec context.
const void * aom_codec_iter_t
Iterator.
Definition aom_codec.h:305
aom_codec_err_t aom_codec_peek_stream_info(aom_codec_iface_t *iface, const uint8_t *data, size_t data_sz, aom_codec_stream_info_t *si)
Parse stream info from a buffer.
struct aom_codec_stream_info aom_codec_stream_info_t
Initialization-time Feature Enabling.
aom_image_t * aom_codec_get_frame(aom_codec_ctx_t *ctx, aom_codec_iter_t *iter)
Decoded frames iterator.
aom_codec_err_t aom_codec_decode(aom_codec_ctx_t *ctx, const uint8_t *data, size_t data_sz, void *user_priv)
Decode data.
struct aom_codec_dec_cfg aom_codec_dec_cfg_t
Initialization Configurations.
#define aom_codec_dec_init(ctx, iface, cfg, flags)
Convenience macro for aom_codec_dec_init_ver()
Definition aom_decoder.h:129
const char * name
Definition aom_codec.h:316
unsigned int threads
Definition aom_decoder.h:92
uint8_t * data
Definition aom_frame_buffer.h:41
size_t size
Definition aom_frame_buffer.h:42
void * priv
Definition aom_frame_buffer.h:43
unsigned int h
Definition aom_decoder.h:73
unsigned int w
Definition aom_decoder.h:72
unsigned int bit_depth
Definition aom_image.h:210
aom_chroma_sample_position_t csp
Definition aom_image.h:204
aom_img_fmt_t fmt
Definition aom_image.h:199
int stride[3]
Definition aom_image.h:232
unsigned int d_w
Definition aom_image.h:213
int monochrome
Definition aom_image.h:203
unsigned int d_h
Definition aom_image.h:214
aom_color_range_t range
Definition aom_image.h:205
unsigned char * planes[3]
Definition aom_image.h:231